//
//  COGiftViewController.swift
//  CocoProject
//
//  Created by Ruthless on 2023/8/23.
//

import UIKit

class COGiftViewController: COBaseViewController {
    private var animationDelegate: COPresentAnimationDelegate = COPresentAnimationDelegate()
    
    var okAction: (()->Void)?
    
    var id: Int = 0
    
    var clickAction: ((Int)->Void)?
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nil, bundle: nil)
        self.modalPresentationStyle = .custom
        self.transitioningDelegate = animationDelegate
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override var navShow: Bool {
        false
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    override func configUI() {
        
        view.addSubview(mainView)
        mainView.addSubview(backImageView)
        mainView.addSubview(giftLabel)
        mainView.addSubview(closeButton)
        mainView.addSubview(oneView)
        mainView.addSubview(twoView)
        mainView.addSubview(threeView)
        mainView.addSubview(sendButton)
        
        mainView.snp.makeConstraints { make in
//            make.left.right.bottom.equalToSuperview()
            make.edges.equalToSuperview()
            make.height.equalTo(262+bottomSafeHeight)
        }
        
        backImageView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(-1)
            make.bottom.top.equalToSuperview().offset(0)
        }
        
        giftLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(17)
        }
        
        closeButton.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-15)
            make.centerY.equalTo(giftLabel)
        }
        
        oneView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(42)
            make.top.equalTo(giftLabel.snp.bottom).offset(30)
            make.width.equalTo(80)
            make.height.equalTo(121)
        }
        
        twoView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(giftLabel.snp.bottom).offset(30)
            make.width.equalTo(80)
            make.height.equalTo(121)
        }
        
        threeView.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-42)
            make.top.equalTo(giftLabel.snp.bottom).offset(30)
            make.width.equalTo(80)
            make.height.equalTo(121)
        }
        
        sendButton.snp.makeConstraints { make in
            make.width.equalTo(130)
            make.height.equalTo(34)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-8-bottomSafeHeight)
        }
    }

    private lazy var mainView: UIView = {
        let view = UIView()
        view.backgroundColor = .clear
        return view
    }()
    
    private lazy var backImageView: UIImageView = {
        let imgV = UIImageView(image: UIImage(named: "矩形"))
        return imgV
    }()
    
    private lazy var giftLabel: UILabel = {
        let label = UILabel()
        label.font = .medium(25)
        label.textColor = .white
        label.text = "Gift"
        return label
    }()
    
    private lazy var closeButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "编组 5"), for: .normal)
        btn.addTarget(self, action: #selector(back), for: .touchUpInside)
        return btn
    }()
    
    private lazy var oneView: COGiftView = {
        let view = COGiftView()
        view.sel = true
        view.flowImageView.image = UIImage(named: "位图备份 3")
        view.nameLabel.text = "1 Flower"
        view.moneyButton.setTitle("6", for: .normal)
        view.clickAction = {[weak self] in
            self?.oneView.sel = true
            self?.twoView.sel = false
            self?.threeView.sel = false
        }
    
        return view
    }()
    
    private lazy var twoView: COGiftView = {
        let view = COGiftView()
        view.flowImageView.image = UIImage(named: "编组 15")
        view.nameLabel.text = "3 Flower"
        view.moneyButton.setTitle("16", for: .normal)
        
        view.clickAction = {[weak self] in
            self?.oneView.sel = false
            self?.twoView.sel = true
            self?.threeView.sel = false
        }
        return view
    }()
    
    private lazy var threeView: COGiftView = {
        let view = COGiftView()
        view.flowImageView.image = UIImage(named: "编组 15")
        view.nameLabel.text = "8 Flower"
        view.moneyButton.setTitle("30", for: .normal)
        
        view.clickAction = {[weak self] in
            self?.oneView.sel = false
            self?.twoView.sel = false
            self?.threeView.sel = true
        }
        return view
    }()
    
    private lazy var sendButton: UIButton = {
        let btn = UIButton()
        let bgLayer1 = CAGradientLayer()
        bgLayer1.colors = [UIColor.rgba(218, 67, 255).cgColor, UIColor.rgba(82, 28, 255).cgColor]
        bgLayer1.locations = [0, 1]
        bgLayer1.frame = CGRect(x: 0, y: 0, width: 130, height: 34)
        bgLayer1.startPoint = CGPoint(x: 1, y: 0.56)
        bgLayer1.endPoint = CGPoint(x: 0.56, y: 0.56)
        btn.layer.insertSublayer(bgLayer1, at: 0)
        
        btn.setTitle("Send", for: .normal)
        btn.titleLabel?.font = .medium(15)
        btn.setTitleColor(.white, for: .normal)
        
        btn.layer.masksToBounds = true
        btn.layer.cornerRadius = 17
        btn.addTarget(self, action: #selector(sendGift), for: .touchUpInside)
        return btn
    }()
}

extension COGiftViewController {
    @objc
    private func back() {
        self.dismiss(animated: true)
    }
    
    @objc
    private func sendGift() {
        var count = 1
        if oneView.sel {
            count = 1
        } else if twoView.sel {
            count = 3
        } else if threeView.sel {
            count = 8
        }
        
        self.view.makeToastActivity(.center)
        CONetwork.sendRequest(provider: heProvider, target: .gift(id: id, count: count)) { _ in
            self.view.hideToastActivity()
            self.okAction?()
        } failedBlock: { _, text in
            self.view.hideToastActivity()
            self.view.makeToast(text, position: .center)
        }

    }
}
